bitmasks brute force dp *1200

Please click on ads to support us..

Python Code:

n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
for i in range(0,2**n):
    res=0
    for j in range(n):
        if(i &(2**j)):
            res+=l[j]
        else:
            res-=l[j]
    if res%360==0:
        print("yes")
        break
else:
    print("no")
    

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<numeric>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
#define all(v)          v.begin(),v.end()
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void Never_give_up() {
    std::ios_base::sync_with_stdio(NULL);
    cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE 
    freopen("in.txt", "r", stdin);
#endif
}
void solve() {
    int n;  cin >> n;
    bool Ok = false;
    vi v(n);  for (auto& it : v) cin >> it;
    for (int mask = 0; mask < (1 << n); mask++) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (mask & (1 << i)) sum += v[i];
            else sum -= v[i];
        }
        Ok |= (sum % 360 == 0);
    }
    cout << ((Ok) ? "YES" : "NO");
}
int main() {
    //وَلَسَوْفَ يُعْطِيكَ رَبُّكَ فَتَرْضَى
    Never_give_up();
    int T = 1;  //cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
	  					  			 			 	  			 		 	 	


Comments

Submit
0 Comments
More Questions

12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String
1708B - Difference of GCDs
863A - Quasi-palindrome
1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers